using CameraManager.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace CameraManager.ViewModels;

public partial class MainWindowViewModel : ObservableObject
{
    private readonly CameraManager.Models.CameraManager _cameraManager;

    [ObservableProperty] private string _outputDirectory =
        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "CameraApp");

    [ObservableProperty] private string _statusMessage = "就绪";

    public ObservableCollection<CameraDevice> Cameras => _cameraManager.Cameras;

    public MainWindowViewModel(CameraManager.Models.CameraManager cameraManager)
    {
        _cameraManager = cameraManager;
        InitializeAsync();
    }

    private async void InitializeAsync()
    {
        try
        {
            StatusMessage = "正在检测摄像头...";
            await _cameraManager.InitializeAsync();
            StatusMessage = $"检测到 {Cameras.Count} 个摄像头";
        }
        catch (Exception ex)
        {
            StatusMessage = $"初始化失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private async Task RefreshCameras()
    {
        InitializeAsync();
    }

    [RelayCommand]
    private async Task TakeAllPicture()
    {
        try
        {
            StatusMessage = "所有摄像头正在拍照...";
            foreach (var camera in Cameras)
            {
                TakePicture(camera);
            }

            StatusMessage = "所有摄像头已拍照";
        }
        catch (Exception ex)
        {
            StatusMessage = $"拍照失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private async Task StartAllRecording()
    {
        try
        {
            StatusMessage = "正在开启所有摄像头录像...";
            foreach (var camera in Cameras)
            {
                StartRecording(camera);
            }

            StatusMessage = "已开启所有摄像头录像";
        }
        catch (Exception ex)
        {
            StatusMessage = $"开启录像失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private async Task StopAllRecording()
    {
        try
        {
            StatusMessage = "正在停止所有摄像头录像...";
            foreach (var camera in Cameras)
            {
                StopRecording(camera);
            }

            StatusMessage = "已停止所有摄像头录像";
        }
        catch (Exception ex)
        {
            StatusMessage = $"停止录像失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private async Task StopAllCameras()
    {
        try
        {
            StatusMessage = "正在停止所有摄像头...";
            await _cameraManager.StopAllCamerasAsync();
            StatusMessage = "所有摄像头已停止";
        }
        catch (Exception ex)
        {
            StatusMessage = $"停止失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private void OpenDirectory()
    {
        try
        {
            if (!Directory.Exists(OutputDirectory))
                Directory.CreateDirectory(OutputDirectory);

            // 在 Linux 上打开目录
            System.Diagnostics.Process.Start("xdg-open", OutputDirectory);
            StatusMessage = "已打开输出目录";
        }
        catch (Exception ex)
        {
            StatusMessage = $"打开目录失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private async Task StartCamera(CameraDevice camera)
    {
        try
        {
            StatusMessage = $"正在启动 {camera.Name}...";
            await _cameraManager.StartCameraAsync(camera);
            StatusMessage = $"{camera.Name} 已启动";
        }
        catch (Exception ex)
        {
            StatusMessage = $"启动失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private async Task StopCamera(CameraDevice camera)
    {
        try
        {
            StatusMessage = $"正在停止 {camera.Name}...";
            await _cameraManager.StopCameraAsync(camera);
            StatusMessage = $"{camera.Name} 已停止";
        }
        catch (Exception ex)
        {
            StatusMessage = $"停止失败: {ex.Message}";
        }
    }


    [RelayCommand]
    private void TakePicture(CameraDevice camera)
    {
        try
        {
            if (!Directory.Exists(OutputDirectory))
                Directory.CreateDirectory(OutputDirectory);

            var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
            var filePath = Path.Combine(OutputDirectory, $"{camera.Name}_{timestamp}.jpg");

            camera.TakePicture(filePath);
            StatusMessage = $"照片已保存: {filePath}";
        }
        catch (Exception ex)
        {
            StatusMessage = $"拍照失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private void StartRecording(CameraDevice camera)
    {
        try
        {
            if (!Directory.Exists(OutputDirectory))
                Directory.CreateDirectory(OutputDirectory);

            camera.StartRecording(OutputDirectory);
            StatusMessage = $"{camera.Name} 开始录制...";
        }
        catch (Exception ex)
        {
            StatusMessage = $"开始录制失败: {ex.Message}";
        }
    }

    [RelayCommand]
    private void StopRecording(CameraDevice camera)
    {
        try
        {
            camera.StopRecording();
            StatusMessage = $"{camera.Name} 录制已停止";
        }
        catch (Exception ex)
        {
            StatusMessage = $"停止录制失败: {ex.Message}";
        }
    }
}